#pragma once
#include <iostream>
#include <string>
#include <cerrno>
#include <stdio.h>
using namespace std;

namespace MyTask
{
    class Task
    {
    private:
        int _sock;

    public:
        Task(){};
        Task(int sock)
            : _sock(sock){};

        void operator()()
        {
            Run();
        }

        ~Task(){};

    private:
        void Run()
        {
            // version 1
            while (true)
            {
                char buffer[1024];
                memset(buffer, 0, sizeof(buffer));
                ssize_t s = read(_sock, buffer, sizeof(buffer));
                if (s > 0)
                {
                    buffer[s] = 0;
                    cout << "server # " << buffer;

                    string message = ">>>server<<< ";
                    message += buffer;
                    write(_sock, message.c_str(), message.size());
                }
                else if (s == 0)
                {
                    cout << "client quit..." << endl;
                    break;
                    ;
                }
                else
                {
                    cerr << "read error" << errno << endl;
                    break;
                }
            }
        }
    };
}

